<template>
    <div class="manage-management">
        <div style="display: flex;justify-content: space-between;" fit>
            <!-- 添加帮助 -->
            <el-button type="primary" @click="addHelp">添加帮助</el-button>
            <!-- 搜索帮助 -->
            <div style="display: flex;">
                <el-input v-model="filterKeyword" placeholder="请输入问题"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </div>
        </div>
        <!-- 列表信息 -->
        <el-table :data="filteredHelp" style="width: 100%;margin-top: 20px;" fit border>
            <el-table-column label="问题" align="center">
                <template slot-scope="scope">
                    <p style="margin-left: 10px">{{ scope.row.question }}</p>
                </template>
            </el-table-column>
            <el-table-column label="答案" align="center">
                <template slot-scope="scope">
                    <p v-if="scope.row.phone != ''">{{ scope.row.answer }}</p>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="{row}">
                    <el-button type="primary" size="small" @click="editHelp(row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="deleteHelp(row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @size-change="handlePageSizeChange" @current-change="handleCurrentPageChange"
            :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" :total="total"
            layout="total, sizes, prev, pager, next"></el-pagination>
        <!-- 弹出框 -->
        <el-dialog :visible.sync="dialogVisible" title="帮助管理">
            <el-form :model="editedHelp" label-width="100px">
                <el-form-item label="问题">
                    <el-input v-model="editedHelp.question"></el-input>
                </el-form-item>
                <el-form-item label="答案">
                    <el-input v-model="editedHelp.answer"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="saveHelp">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            help: [],// 获取的数据
            filteredHelp: [],// 获取的数据
            filterKeyword: '',// 查找输入框的内容
            currentPage: 1, // 当前页
            pageSize: 10, // 当前页的条数
            dialogVisible: false, // 弹出框是否可视
            editedHelp: {},// 修改用户信息的数据
            total: 0, // 总条数
        }
    },
    methods: {
        // 获取帮助信息
        filterHelp() {
            const start = (this.currentPage - 1) * this.pageSize;
            const end = start + this.pageSize;
            this.$http
                .get('/gethelp')
                .then(res => {
                    this.help = res.data;
                    this.filteredHelp = res.data.slice(start, end)
                    this.total = res.data.length
                })
        },
        // 搜索帮助信息
        search() {
            const keyword = this.filterKeyword.trim().toLowerCase()
            if (keyword == '') {
                this.$message.error("请输入问题")
            } else {
                const start = (this.currentPage - 1) * this.pageSize;
                const end = start + this.pageSize;
                this.$http
                    .post('/selhelp', { question: keyword })
                    .then(res => {
                        if (res.data == undefined) {
                            this.filteredHelp = res.data
                            this.$message.error(res.status)
                        } else {
                            this.filteredHelp = res.data.slice(start, end)
                            this.total = res.data.length
                            this.$message.success(res.status)
                        }
                    })
            }
        },
        // 打开增加帮助信息表单
        addHelp() {
            this.editedHelp = { question: '', answer: '' }
            this.dialogVisible = true
        },
        // 打开编辑帮助信息表单
        editHelp(help) {
            this.editedHelp = { ...help }
            this.dialogVisible = true
        },
        // 保存帮助信息
        saveHelp() {
            if (this.editedHelp.question.trim() === '') {
                this.$message.error('问题不能为空')
                return
            }
            if (this.editedHelp.answer.trim() === '') {
                this.$message.error('回答不能为空')
                return
            }
            if (this.editedHelp.hasOwnProperty('id')) {
                // 修改帮助信息
                this.$http
                    .post('/edithelp', this.editedHelp)
                    .then(res => {
                        this.$message.success(res.status)
                    })
            } else {
                // 增加用户信息
                this.$http
                    .post('/addhelp', this.editedHelp)
                    .then(res => {
                        this.$message.success(res.status)
                    })
            }
            if (!this.filterKeyword) {
                this.filterHelp()
            } else {
                this.search()
            }
            this.dialogVisible = false
        },
        // 删除帮助信息
        deleteHelp(help) {
            this.$confirm(`确定要删除帮助 ${help.question} 吗？`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.$http
                    .post('/delhelp', help)
                    .then(res => {
                        this.$message.success(res.status)
                        if (!this.filterKeyword) {
                            this.filterHelp()
                        } else {
                            this.search()
                        }
                    })
            })
        },
        // 切换每页条数
        handlePageSizeChange(size) {
            this.pageSize = size
            this.currentPage = 1
            this.filterManage()
        },
        // 切换页码
        handleCurrentPageChange(page) {
            this.currentPage = page
            this.filterManage()
        },
    },
    mounted() {
        this.filterHelp()
    },
}
</script>